package com.huaxin.dss2.util;

import com.google.common.collect.Maps;
import com.huaxin.dss2.Launcher;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by HX-119 on 2014/5/9.
 */
@Slf4j
public class PropertiesUtil {


    private static Properties properties = new Properties();
    private static Properties sqlProperties = new Properties();

    static {
        File propertiesFile = new File(Launcher.APP_PATH+File.separator+"conf"+File.separator+"config.properties");
        try (FileInputStream fis = new FileInputStream(propertiesFile)){
            properties.load(fis);
        } catch (IOException e) {
            log.error(e.getMessage());
        }

        File sqlPropertiesFile = new File(Launcher.APP_PATH+File.separator+"conf"+File.separator+"sql-config.properties");
        try (FileInputStream fis = new FileInputStream(sqlPropertiesFile)){
            sqlProperties.load(fis);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    private PropertiesUtil() {

    }

    public static String getString(String propertyName) {
        return properties.getProperty(propertyName);
    }

    public static String getSql(String propertyName) {
        return sqlProperties.getProperty(propertyName);
    }

    public static int getInt(String propertyName) {
        String property = properties.getProperty(propertyName);
        return Integer.valueOf(property);
    }

    public static boolean getBoolean(String propertyName){
        String property = properties.getProperty(propertyName);
        return Boolean.parseBoolean(property);
    }

    public static Map<String,String> getPreProperty(String pre) {
        Map<String, String> map = Maps.newHashMap();
        sqlProperties.stringPropertyNames().stream().filter((n) -> n.startsWith(pre))
            .forEach((v -> map.put(v, sqlProperties.getProperty(v))));
        properties.stringPropertyNames().stream().filter((n) -> n.startsWith(pre))
            .forEach((v -> map.put(v, properties.getProperty(v))));
        return map;
    }

}
